package com.worldtolearn.user.actions;

import java.io.Serializable;

import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.worldtolearn.comment.bu.CommentBu;
import com.worldtolearn.comment.bu.ICommentBu;
import com.worldtolearn.comment.dao.CommentDAO;
import com.worldtolearn.comment.dao.CommentDAOImpl;
import com.worldtolearn.comment.entity.Comment;
import com.worldtolearn.user.entity.User;

import fr.awtl.framework.actions.Action;
import fr.awtl.framework.actions.forms.ActionForm;

public class SignInAction implements Action, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Comment commentO = new Comment();


	@Override
	public String execute(ActionForm actionForm, HttpServletRequest req,
			HttpServletResponse res) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"../springConf.xml");
		CommentDAO ctDao = applicationContext.getBean(CommentDAOImpl.class);
		HttpSession session = req.getSession(false);

		User user = (session != null) ? (User) session.getAttribute("user")
				: null;

		if (user != null) {

			commentO.setPerson(user);
			commentO.setComment(req.getParameter("comment"));
			if (ctDao != null) {
				ctDao.addComment(commentO);
			} else {
				System.out.println("le ctDao est null");
			}
		}

		return "/pages/thankComment.xhtml";
	}
}
